<template>
  <div class="detail">
    <MesHeader title="请购明细" icon="el-icon-s-grid"></MesHeader>
    <MesTable ref="detail" :tableData="tableData" :columns="columns"></MesTable>
    <ReviewRequisition
      ref="requestModel"
      @parentCall="parentCall"
    ></ReviewRequisition>
  </div>
</template>

<script setup>
import MesTable from '@/components/basic/MesTable.vue'
import MesHeader from '@/components/basic/MesHeader.vue'
import ReviewRequisition from './ReviewRequisition'
import { getCurrentInstance, ref } from 'vue'
import { getNowDate } from '@/uitils/dateFormatUtil'

const emit = defineEmits(['parentCall'])
const requestModel = ref(null)
const columns = [
  {
    title: '合同编号',
    field: 'ContractCode',
    width: 150
  },
  {
    title: '订单号',
    field: 'MoCode',
    width: 150
  },
  {
    title: '物料编号',
    field: 'InvCode',
    width: 150
  },
  {
    title: '物料名称',
    field: 'InvName',
    width: 150
  },
  {
    title: '物料规格',
    field: 'InvStandard',
    width: 150
  },
  // {
  //   title: '总缺料数',
  //   field: 'OrgQty',
  // },
  {
    title: '数量',
    field: 'Qty',
  },
  {
    title: '状态',
    field: 'Status',
    bind: {
      key: 'inStoreState',
      data: [],
    },
  },
  {
    title: '审核人',
    field: 'Verifier',
  },
  {
    title: '审核时间',
    field: 'VerifyDate',
  },
  {
    title: '需求日期',
    field: 'RequireDate',
  },
]
const currentRow = ref(null)
const detail = ref(null)
const that = getCurrentInstance().appContext.config.globalProperties
const instance = getCurrentInstance()
let tableData = ref([])
const rowClick = (row) => {
  console.log(row)
  currentRow.value = row
  getDetailData(row.Req_Id)
}

const getDetailData = async (id) => {
  let res = await that.http.get(
    `api/Production_Requisition/getDetailRows?Req_Id=${id}`,
    null,
    true
  )
  // console.log(res)
  tableData.value = res
}

//转采购单
const TransferPurchase = async () => {
  try {
    const select = detail.value.getSelected()
    if (select.length === 0) return that.$message.error('请选择数据')
    const sendTableData = select.map((item) => {
      if (item.Status == 0) throw new Error('请选择已审核的数据')
      return {
        ...item,
        ActualQty: item.Qty,
      }
    })
    const fields = {
      MoCode: select[0].MoCode,
      CreateTime: getNowDate(),
      ExchangeRate: '1',
      Currency: 'CNY',
      TaxRate: '1.13',
    }
    requestModel.value.openModel()
    requestModel.value.setFormFields(fields)
    requestModel.value.setSendTableData(sendTableData)
  } catch (e) {
    that.$message.warning(e.message)
  }
}

//审核
const audit = async () => {
  try {
    const select = detail.value.getSelected()
    if (select.length === 0) return that.$message.error('请选择数据')
    const sendTableData = select.map((item) => {
      if (item.Status == 1) throw new Error('请选择未审核的数据')
      return {
        reqList_Id: item.ReqList_Id,
      }
    })
    const res = await that.http.post(
      '/api/Production_Requisition/UpdateState',
      sendTableData,
      true
    )
    if (res.status) {
      that.$message.success('审核成功')
      emit('parentCall', ($parent) => {
        $parent.search()
      })
    } else {
      that.$message.success(res.message)
    }
  } catch (e) {
    that.$message.warning(e.message)
  }
}

const parentCall = () => {
  rowClick(currentRow.value)
}

//打印
const printData = () => {
  let detailData = detail.value.getSelected()
  return detailData
}

defineExpose({ rowClick, TransferPurchase, printData, audit })
</script>

<style lang="less" scoped>
.detail {
  padding: 15px;
}
</style>
